package com.chengyu.core.controller.goods;

import com.chengyu.core.component.DemoStationForbid;
import com.chengyu.core.component.OperationLog;
import com.chengyu.core.controller.ShopBaseController;
import com.chengyu.core.domain.form.GoodsDtoForm;
import com.chengyu.core.domain.form.GoodsSearchForm;
import com.chengyu.core.domain.result.GoodsResult;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.RecommendGoods;
import com.chengyu.core.service.goods.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @title  推荐商品
 * @author LeGreen
 * @date   2022/5/7
 */
@Api(tags = "推荐商品")
@Controller
@RequestMapping("/member")
public class GoodsRecommendController extends ShopBaseController {
	
	@Autowired
	private GoodsService goodsService;

	
	@ApiOperation(value = "推荐商品列表")
	@ResponseBody
	@RequestMapping(value="/goodsRecommend/getList", method=RequestMethod.GET)
	public CommonResult<CommonPage<GoodsDtoForm>> getList(GoodsSearchForm form,
														  @RequestParam(value = "page", defaultValue = "1") Integer page,
														  @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
		form.setShopId(getCurrentShopId());
		List<GoodsDtoForm> list = goodsService.getNiceGoodsList(form, page, pageSize);
		return CommonResult.success(CommonPage.restPage(list));
	}

	@OperationLog
	@ApiOperation(value = "修改排序-推荐商品")
	@ResponseBody
	@RequestMapping(value="/goodsRecommend/updateSort", method=RequestMethod.POST)
	public CommonResult<String> editSubmit(Integer id, Integer sort) throws ServiceException {
		RecommendGoods recommendGoods = new RecommendGoods();
		recommendGoods.setId(id);
		recommendGoods.setSort(sort);
		goodsService.updateRecommendGoods(recommendGoods);
		return CommonResult.success(null);
	}



	@DemoStationForbid
	@OperationLog
	@ApiOperation(value = "删除商品")
	@ResponseBody
	@RequestMapping(value="/goodsRecommend/delete", method=RequestMethod.POST)
	public CommonResult<String> delete(String ids) throws ServiceException {
		if(StringUtils.isEmpty(ids)) {
			return CommonResult.failed("请至少选择一条数据");
		}
		List<Integer> goodsIdList = getIdList(ids);
		goodsService.deleteGoodsRecommend(goodsIdList);
		return CommonResult.success(null);
	}

	@ApiOperation(value = "商品详情")
	@ResponseBody
	@RequestMapping(value="/goodsRecommend/get", method=RequestMethod.GET)
	public CommonResult<GoodsResult> get(Integer goodsId) {
		GoodsResult goods = goodsService.getGoodsResult(goodsId);
		return CommonResult.success(goods);
	}

	@OperationLog
	@ApiOperation(value = "批量添加")
	@ResponseBody
	@RequestMapping(value="/goodsRecommend/batchAdd", method=RequestMethod.POST)
	public CommonResult<String> batchAdd(String ids) throws ServiceException {
		if(StringUtils.isEmpty(ids)) {
			return CommonResult.failed("请至少选择一条数据");
		}
		List<Integer> goodsIdList = getIdList(ids);
		goodsService.batchAddRecommendGoods(goodsIdList,getCurrentShop().getName(),true);
		return CommonResult.success(null);
	}
	@OperationLog
	@ApiOperation(value = "批量添加-通过虚拟币")
	@ResponseBody
	@RequestMapping(value="/goodsRecommend/batchAddByVirtualCurrency", method=RequestMethod.POST)
	public CommonResult<String> batchAddByVirtualCurrency(Integer selectBuyDayType,Integer chooseGoodsId) throws ServiceException {
		if(chooseGoodsId==null) {
			return CommonResult.failed("请至少选择一条数据");
		}
		if(selectBuyDayType==null) {
			return CommonResult.failed("请选择一种支付项目");
		}
		goodsService.batchAddRecommendGoodsByVirtualCurrency(chooseGoodsId,selectBuyDayType,getCurrentShop().getName(),getCurrentShopId());
		return CommonResult.success(null);
	}



}